
#include "MyCameraWindow.h"
#include <iostream>
#include <QDir>
#include <QMessageBox>


MyCameraWindow::MyCameraWindow( QWidget *parent) : QWidget(parent) {
    camera = cvCreateCameraCapture(0);//creating an instance of the camera
    if (camera == 0)//check to see if another inctance of the camera wants to be opened
    {
        QMessageBox msg;
        msg.setIcon(QMessageBox::Critical);
        msg.setWindowTitle(tr("Critical Error!"));//meaasge diaplayed
        msg.setText(tr("You're running another instance of this program, "
                       "or some other program is accessing the webcam."));//message displayed
        msg.setButtonText(0,tr("Quit"));
        msg.exec();//wmessage box shown
        exit(0);
    }
    assert(camera);
    IplImage * image=cvQueryFrame(camera);//creating an image equal to the image obtained from the camera
    assert(image);

    //printf("Image depth=%i\n", image->depth);
    //printf("Image nChannels=%i\n", image->nChannels);

    QVBoxLayout *layout = new QVBoxLayout;//creating a layout
    cvwidget = new QOpenCVWidget(this);//initializing an instance of QOpenCVWidget
    layout->addWidget(cvwidget);//adding it to the layout
    setLayout(layout);//setting the widgets layout
    //resize(300, 200);

    startTimer(100);  // 0.01-second timer
 }
/*
he timer event refreshes the image from the camera
*/
void MyCameraWindow::timerEvent(QTimerEvent*) {
    IplImage *image=cvQueryFrame(camera);
    cvwidget->putImage(image);
}
/*
this function is used to record the video from the camera
*/
void MyCameraWindow::record(QString filename)
{
    emit stopRecordButton(true);


    CvVideoWriter *writer = 0;//ceating an instance of the cvVideoWrite

    int isColor = 1;//setting if the camera is colour or not
    int fps     = 25;  // or 30
    int frameW  = 640; // 744 for firewire cameras
    int frameH  = 480; // 480 for firewire cameras
    QDir test;//creating a qdir object
    test.remove(filename);//deleting the previously recorded file
    //std::cout << writer << std::endl;
    writer = cvCreateAVIWriter(filename.toStdString().c_str(),CV_FOURCC('P','I','M','1'),
                               fps,cvSize(frameW,frameH),isColor);//initializing the writer with the arguments

    assert(writer);//creating an instance of IplImage
    IplImage* img = 0;
    i = 0;
    while(i!=1){
        cvGrabFrame(camera);          // capture a frame
        img=cvRetrieveFrame(camera);  // retrieve the captured frame
        assert(img);
        cvFlip(img, NULL ,1);//flipping the image for the vriting

        cvWriteFrame(writer,img);      // add the frame to the file
        cvWriteFrame(writer,img);//adding more frames to the video
        cvWriteFrame(writer,img);//adding more frames to the video
        cvShowImage( "mainWin", img );//previewing the recorded images

        cvWaitKey(40) ;//waiting 40 mili sec
    }
    cvReleaseVideoWriter(&writer);//releasing the video writer
    //delete writer;
}
/*
function used to stop the recording from the webcam
*/
void MyCameraWindow::stopRec()
{
    i=1;
    emit stopRecordButton(false);
}
